<?php

namespace Handler;

class SFSHandler {
	/**
	 * 单例
	 *
	 * @var SFSHandler
	 */
	private static $m_Instance;
	/**
	 *
	 * @var \Req_sfslobby
	 */
	private $m_ReqSFS;
	/**
	 *
	 * @var string
	 */
	private $m_ReqErrMsg;
	/**
	 * 构造函数
	 */
	private function __construct() {
		$this->m_ReqSFS = new \Req_sfslobby ();
		$this->m_ReqErrMsg = '外部服务器异常，请稍后再试！';
	}
	/**
	 * 复制方法
	 */
	private function __clone() {
	}
	/**
	 * 获取单例
	 *
	 * @return SFSHandler
	 */
	public static function mb_GetInstance() {
		if (! (self::$m_Instance instanceof self)) {
			self::$m_Instance = new self ();
		}
		return self::$m_Instance;
	}
	/**
	 * 请求注册
	 *
	 * @param unknown $name        	
	 * @param unknown $nick        	
	 * @param unknown $pass        	
	 * @return \stdClass
	 */
	public function mb_RegistToSFS($name, $nick, $pass, $agent, $phone) {
		return $this->mb_DecodeSFSData ( $this->m_ReqSFS->AccountRegist ( $name, $nick, $pass, $agent, $phone ) );
	}
	/**
	 * 请求登录
	 *
	 * @param unknown $name        	
	 * @param unknown $pass        	
	 * @return stdClass
	 */
	public function mb_LoginToSFS($name, $pass) {
		return $this->mb_DecodeSFSData ( $this->m_ReqSFS->AccountLogin ( $name, $pass ) );
	}
	/**
	 * 查询数据
	 *
	 * @param unknown $name        	
	 * @return stdClass
	 */
	public function mb_QueryDataToSFS($name) {
		return $this->mb_DecodeSFSData ( $this->m_ReqSFS->QueryAccountData ( $name ) );
	}
	/**
	 * 保存金币
	 *
	 * @param unknown $name        	
	 * @param unknown $coin        	
	 * @return stdClass
	 */
	public function mb_SaveDataToSFS($name, $coin, $gameName) {
		return $this->mb_DecodeSFSData ( $this->m_ReqSFS->SaveAccountData ( $name, $coin, $gameName ) );
	}
	/**
	 * 查询用户状态
	 *
	 * @param unknown $name        	
	 * @return stdClass
	 */
	public function mb_QueryStatusToSFS($name) {
		return $this->mb_DecodeSFSData ( $this->m_ReqSFS->QueryAccountStatus ( $name ) );
	}
	/**
	 * 保存用户信息
	 *
	 * @param unknown $name
	 *        	账号
	 * @param unknown $sex        	
	 * @param unknown $con        	
	 * @param unknown $sig        	
	 * @param unknown $loc        	
	 * @param unknown $phone        	
	 * @param unknown $wechat        	
	 * @param unknown $qq        	
	 * @param unknown $email        	
	 * @return stdClass
	 */
	public function mb_SaveUserInfo($name, $sex, $con, $sig, $loc, $phone, $wechat, $qq, $email) {
		return $this->mb_DecodeSFSData ( $this->m_ReqSFS->SaveUserInfo ( $name, $sex, $con, $sig, $loc, $phone, $wechat, $qq, $email ) );
	}
	/**
	 * 查询数据记录
	 *
	 * @param unknown $name
	 *        	账号
	 * @param unknown $type
	 *        	类型（0：充值记录，1：消耗记录）
	 * @return stdClass
	 */
	public function mb_QueryDataRecord($name, $type) {
		return $this->mb_DecodeSFSData ( $this->m_ReqSFS->QueryDataRecord ( $name, $type ) );
	}
	/**
	 * 金币兑换
	 *
	 * @param unknown $name        	
	 * @param unknown $playerName        	
	 * @param unknown $phone        	
	 * @param unknown $platform        	
	 * @param unknown $coins        	
	 * @return stdClass
	 */
	public function mb_CoinsExchange($name, $playerName, $phone, $platform, $coins) {
		return $this->mb_DecodeSFSData ( $this->m_ReqSFS->CoinsExchange ( $name, $playerName, $phone, $platform, $coins ) );
	}
	/**
	 * 绑定代理
	 *
	 * @param unknown $name        	
	 * @param unknown $agent        	
	 * @return stdClass
	 */
	public function mb_BindingAgent($name, $agent) {
		return $this->mb_DecodeSFSData ( $this->m_ReqSFS->BindingAgent ( $name, $agent ) );
	}
	/**
	 * 用户上次进入游戏记录
	 *
	 * @param unknown $name        	
	 * @return stdClass
	 */
	public function mb_UserLastEnterGameRecord($name) {
		return $this->mb_DecodeSFSData ( $this->m_ReqSFS->UserLastEnterGameRecord ( $name ) );
	}
	/**
	 *
	 * @param unknown $name        	
	 * @param unknown $pass        	
	 */
	public function mb_ResetPassword($name, $pass) {
		return $this->mb_DecodeSFSData ( $this->m_ReqSFS->ResetPassword ( $name, $pass ) );
	}
	/**
	 * 获取游戏信息
	 * 
	 * @param unknown $gameId        	
	 * @return stdClass
	 */
	public function mb_GetGameInfo($gameId) {
		return $this->mb_DecodeSFSData ( $this->m_ReqSFS->GetGameInfo ( $gameId ) );
	}
	/**
	 * 获取大厅设置
	 * @return stdClass
	 */
	public function mb_GetLobbySetting() {
		return $this->mb_DecodeSFSData ( $this->m_ReqSFS->GetLobbySetting () );
	}
	/**
	 * 解析sfs数据
	 *
	 * @param string $reqData        	
	 * @return \stdClass
	 */
	private function mb_DecodeSFSData($reqData) {
		$data = new \stdClass ();
		if ($reqData == null || $reqData == '') {
			$data->state = false;
			$data->msg = $this->m_ReqErrMsg;
			return $data;
		}
		$reqJson = json_decode ( $reqData );
		if (json_last_error () != JSON_ERROR_NONE) {
			$data->state = false;
			$data->msg = $this->m_ReqErrMsg;
			return $data;
		}
		$state = $reqJson->state;
		if ($state == 'false') {
			$data->state = false;
			$data->msg = $reqJson->msg;
			return $data;
		}
		$data->state = true;
		if (isset ( $reqJson->msg ))
			$data->msg = $reqJson->msg;
		if (isset ( $reqJson->data ))
			$data->data = $reqJson->data;
		return $data;
	}
}